package com.lyx.raindrop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lyx.framework.helper.GlideHelper;
import com.lyx.raindrop.R;
import com.lyx.raindrop.model.AddFriendModel;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;

public class AddFriendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //标题
    public static final int TYPE_TITLE = 0;
    //内容
    public static final int TYPE_CONTENT = 1;

    private IOnItemClickListener mListener;

    List<AddFriendModel> mList;
    private Context mContext;

    public AddFriendAdapter(Context context, List<AddFriendModel> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void setOnItemClickListener(IOnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == TYPE_TITLE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_search_title_item, parent, false);
            viewHolder = new TitleViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_search_user_item, parent, false);
            viewHolder = new ContentViewHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AddFriendModel model = mList.get(position);
        if (holder instanceof TitleViewHolder) {
            ((TitleViewHolder) holder).tv_title.setText(model.getTitle());
        } else if (holder instanceof ContentViewHolder) {
            GlideHelper.loadPicture(mContext, model.getPhoto(), ((ContentViewHolder) holder).iv_photo);
            if (model.getSex()) {
                ((ContentViewHolder) holder).iv_sex.setImageResource(R.drawable.img_boy_icon);
            } else {
                ((ContentViewHolder) holder).iv_sex.setImageResource(R.drawable.img_girl_icon);
            }
            ((ContentViewHolder) holder).tv_nickname.setText(model.getNickName());
            ((ContentViewHolder) holder).tv_age.setText(String.valueOf(model.getAge()));
            ((ContentViewHolder) holder).tv_desc.setText(model.getDesc());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClickListener(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    static class TitleViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;

        public TitleViewHolder(View view) {
            super(view);
            tv_title = view.findViewById(R.id.tv_title);
        }

    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView iv_photo;
        private ImageView iv_sex;
        private TextView tv_nickname;
        private TextView tv_age;
        private TextView tv_desc;

        public ContentViewHolder(View view) {
            super(view);
            iv_photo = view.findViewById(R.id.iv_photo);
            iv_sex = view.findViewById(R.id.iv_sex);
            tv_nickname = view.findViewById(R.id.tv_nickname);
            tv_age = view.findViewById(R.id.tv_age);
            tv_desc = view.findViewById(R.id.tv_desc);
        }

    }

    public interface IOnItemClickListener {
        void onItemClickListener(int position);
    }

}
